using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace ColorPicker
{
	public partial class ColorPickerControlView : UserControl, IComponentConnector
	{
		public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerControlView), new UIPropertyMetadata(Colors.Black));

		public static RoutedUICommand SelectColorCommand = new RoutedUICommand("SelectColorCommand", "SelectColorCommand", typeof(ColorPickerControlView));

		private Window _advancedPickerWindow;

		public Color Color
		{
			get
			{
				return (Color)GetValue(ColorProperty);
			}
			set
			{
				SetValue(ColorProperty, value);
			}
		}

		public ColorPickerControlView()
		{
			base.DataContext = this;
			InitializeComponent();
			base.CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute));
		}

		private void SelectColorCommandExecute(object sender, ExecutedRoutedEventArgs e)
		{
			Color = (Color)ColorConverter.ConvertFromString(e.Parameter.ToString());
		}

		private static void ShowModal(Window advancedColorWindow)
		{
			advancedColorWindow.Owner = Application.Current.MainWindow;
			advancedColorWindow.ShowDialog();
		}

		private void AdvancedPickerPopUpKeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Escape)
			{
				_advancedPickerWindow.Close();
			}
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			popup.IsOpen = false;
			e.Handled = false;
		}

		private void MoreColorsClicked(object sender, RoutedEventArgs e)
		{
			popup.IsOpen = false;
			AdvancedColorPickerDialog advancedColorPickerDialog = new AdvancedColorPickerDialog();
			_advancedPickerWindow = new Window
			{
				AllowsTransparency = true,
				Content = advancedColorPickerDialog,
				WindowStyle = WindowStyle.None,
				ShowInTaskbar = false,
				Background = new SolidColorBrush(Colors.Transparent),
				Padding = new Thickness(0.0),
				Margin = new Thickness(0.0),
				WindowState = WindowState.Normal,
				WindowStartupLocation = WindowStartupLocation.CenterOwner,
				SizeToContent = SizeToContent.WidthAndHeight
			};
			_advancedPickerWindow.DragMove();
			_advancedPickerWindow.KeyDown += AdvancedPickerPopUpKeyDown;
			advancedColorPickerDialog.DialogResultEvent += AdvancedColorPickerDialogDialogResultEvent;
			advancedColorPickerDialog.Drag += AdvancedColorPickerDialogDrag;
			ShowModal(_advancedPickerWindow);
		}

		private void AdvancedColorPickerDialogDrag(object sender, DragDeltaEventArgs e)
		{
			_advancedPickerWindow.DragMove();
		}

		private void AdvancedColorPickerDialogDialogResultEvent(object sender, EventArgs e)
		{
			_advancedPickerWindow.Close();
			DialogEventArgs dialogEventArgs = (DialogEventArgs)e;
			if (dialogEventArgs.DialogResult != DialogResult.Cancel)
			{
				Color = dialogEventArgs.SelectedColor.Color;
			}
		}
	}
}
